ROOTPATH = ../
TOOLPATH = ../../z_tools/
INCPATH	 = ../../z_tools/haribote/
APILIBPATH   = ../apilib/
ZINCCOREPATH = ../core/

MAKE	 = $(TOOLPATH)make.exe -r
NASK 	 = $(TOOLPATH)nask.exe
CC 	 	 = $(TOOLPATH)cc1.exe -I$(INCPATH) -I$(ROOTPATH) -Os -Wall -quiet -std=c99
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM  = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ	 = $(TOOLPATH)bin2obj.exe
BIM2HRB  = $(TOOLPATH)bim2hrb.exe
EDIMG	 = $(TOOLPATH)edimg.exe
IMGTOL 	 = $(TOOLPATH)imgtol.com
GOLIB	 = $(TOOLPATH)golib00.exe
RULEFILE = $(ROOTPATH)zinc.rul
COPY 	 = copy
DEL		 = del

# 默认操作

default :
	$(MAKE) $(APP).hrb

# 文件生成规则

$(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib $(ROOTPATH)make_app.mk Makefile
	$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
		$(APP).obj $(APILIBPATH)apilib.lib

$(APP).hrb : $(APP).bim $(ROOTPATH)make_app.mk Makefile
	$(BIM2HRB) $(APP).bim $(APP).hrb $(MALLOC)

zinc.img : $(ZINCCOREPATH)boot.bin $(ZINCCOREPATH)zinc.sys $(APP).hrb \
		Makefile $(ROOTPATH)make_app.mk
	$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
		wbinimg src:../core/boot.bin len:512 from:0 to:0 \
		copy from:../core/zinc.sys to:@:	\
		copy from:$(APP).hrb to:@: 	\
		imgout:zinc.img

# 一般规则

%.gas : %.c $(ROOTPATH)apilib.h Makefile $(ROOTPATH)make_app.mk
	$(CC) -o $*.gas $*.c

%.nas : %.gas Makefile $(ROOTPATH)make_app.mk
	$(GAS2NASK) $*.gas $*.nas

%.obj : %.nas Makefile $(ROOTPATH)make_app.mk
	$(NASK) $*.nas $*.obj $*.lst

# 伪目标

.PHONY : run all run_all clean src_only

run :
	$(MAKE) zinc.img
	$(COPY) zinc.img ..\..\z_tools\qemu\fdimage0.bin
	$(MAKE) -C $(TOOLPATH)qemu

full :
	$(MAKE) -C $(APILIBPATH)
	$(MAKE) $(APP).hrb

run_all : 
	$(MAKE) -C $(APILIBPATH)
	$(MAKE) -C $(ZINCCOREPATH)
	$(MAKE) run

clean :
	-$(DEL) *.lst
	-$(DEL) *.obj
	-$(DEL) *.map
	-$(DEL) *.bim
	-$(DEL) zinc.img

src_only :
	$(MAKE) clean
	-$(DEL) $(APP).hrb
